// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
// SPDX-License-Identifier: Apache-2.0

package roadmap_dao

import (
	"context"
	"fmt"

	"gitcode.com/lvupclub/lvup-api/dao/common"
	"gitcode.com/lvupclub/proto-gen-go.git/src/roadmap_data_api"
	"go.mongodb.org/mongo-driver/v2/bson"
	"go.mongodb.org/mongo-driver/v2/mongo"
)

type EdgeKeyDbItem struct {
	FromNodeId string `bson:"fromNodeId"`
	FromHandle string `bson:"fromHandle"`
	ToNodeId   string `bson:"toNodeId"`
	ToHandle   string `bson:"toHandle"`
}

type BasicEdgeInfoDbItem struct {
	EdgeType      roadmap_data_api.EDGE_TYPE `bson:"edgeType"`
	HasStartArrow bool                       `bson:"hasStartArrow"`
	HasEndArrow   bool                       `bson:"hasEndArrow"`
	Label         string                     `bson:"label"`
}

type EdgeInfoDbItem struct {
	ObjectId   string              `bson:"_id"`
	RoadmapId  string              `bson:"roadmapId"`
	EdgeKey    EdgeKeyDbItem       `bson:"edgeKey"`
	BasicInfo  BasicEdgeInfoDbItem `bson:"basicInfo"`
	CreateTime int64               `bson:"createTime"`
	UpdateTime int64               `bson:"updateTime"`
}

func (item *EdgeInfoDbItem) ToEdgeInfo() *roadmap_data_api.EdgeInfo {
	return &roadmap_data_api.EdgeInfo{
		EdgeKey: &roadmap_data_api.EdgeKey{
			FromNodeId: item.EdgeKey.FromNodeId,
			FromHandle: item.EdgeKey.FromHandle,
			ToNodeId:   item.EdgeKey.ToNodeId,
			ToHandle:   item.EdgeKey.ToHandle,
		},
		BasicInfo: &roadmap_data_api.BasicEdgeInfo{
			EdgeType:      item.BasicInfo.EdgeType,
			HasStartArrow: item.BasicInfo.HasStartArrow,
			HasEndArrow:   item.BasicInfo.HasEndArrow,
			Label:         item.BasicInfo.Label,
		},
		CreateTime: item.CreateTime,
		UpdateTime: item.UpdateTime,
	}
}

type _EdgeInfoDao struct {
	mc *mongo.Collection
}

func newEdgeInfoDao(db *mongo.Database) (*_EdgeInfoDao, error) {
	mc := db.Collection(common.DB_NS_ROADMAP + "edge")
	_, err := mc.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
		{
			Keys: bson.D{{"roadmapId", 1}, {"edgeKey.fromNodeId", 1}},
		},
		{
			Keys: bson.D{{"roadmapId", 1}, {"edgeKey.toNodeId", 1}},
		},
	})
	if err != nil {
		return nil, err
	}
	return &_EdgeInfoDao{mc: mc}, nil
}

func (db *_EdgeInfoDao) genObjectId(roadmapId string, keyItem *EdgeKeyDbItem) string {
	return fmt.Sprintf("%s:%s:%s:%s:%s", roadmapId, keyItem.FromNodeId, keyItem.FromHandle, keyItem.ToNodeId, keyItem.ToHandle)
}

func (db *_EdgeInfoDao) Insert(ctx context.Context, dbItem *EdgeInfoDbItem) error {
	dbItem.ObjectId = db.genObjectId(dbItem.RoadmapId, &dbItem.EdgeKey)
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_EdgeInfoDao) Exist(ctx context.Context, roadmapId string, keyItem *EdgeKeyDbItem) (bool, error) {
	objectId := db.genObjectId(roadmapId, keyItem)
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": objectId})
	return count > 0, err
}

func (db *_EdgeInfoDao) Get(ctx context.Context, roadmapId string, keyItem *EdgeKeyDbItem) (*EdgeInfoDbItem, error) {
	retItem := &EdgeInfoDbItem{}
	objectId := db.genObjectId(roadmapId, keyItem)
	err := db.mc.FindOne(ctx, bson.M{"_id": objectId}).Decode(retItem)
	return retItem, err
}

func (db *_EdgeInfoDao) Remove(ctx context.Context, roadmapId string, keyItem *EdgeKeyDbItem) error {
	objectId := db.genObjectId(roadmapId, keyItem)
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": objectId})
	return err
}

func (db *_EdgeInfoDao) RemoveByRoadmap(ctx context.Context, roadmapId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"roadmapId": roadmapId})
	return err
}

func (db *_EdgeInfoDao) RemoveByFromNode(ctx context.Context, roadmapId string, fromNodeId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"roadmapId": roadmapId, "edgeKey.fromNodeId": fromNodeId})
	return err
}

func (db *_EdgeInfoDao) RemoveByToNode(ctx context.Context, roadmapId string, toNodeId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"roadmapId": roadmapId, "edgeKey.toNodeId": toNodeId})
	return err
}

func (db *_EdgeInfoDao) List(ctx context.Context, roadmapId string) ([]*EdgeInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"roadmapId": roadmapId})
	if err != nil {
		return nil, err
	}
	retList := []*EdgeInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_EdgeInfoDao) UpdateBasic(ctx context.Context, roadmapId string, keyItem *EdgeKeyDbItem, basicItem *BasicEdgeInfoDbItem, updateTime int64) error {
	objectId := db.genObjectId(roadmapId, keyItem)
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": objectId}, bson.M{
		"$set": bson.M{
			"basicInfo":  basicItem,
			"updateTime": updateTime,
		},
	})
	return err
}
